﻿namespace Xms.Abp.Caching;

public class XmsUnitOfWorkCacheItem<TValue>
{
    public bool IsRemoved { get; set; }

    public TValue Value { get; set; }

    public XmsUnitOfWorkCacheItem()
    {

    }

    public XmsUnitOfWorkCacheItem(TValue value)
    {
        Value = value;
    }

    public XmsUnitOfWorkCacheItem(TValue value, bool isRemoved)
    {
        Value = value;
        IsRemoved = isRemoved;
    }

    public XmsUnitOfWorkCacheItem<TValue> SetValue(TValue value)
    {
        Value = value;
        IsRemoved = false;
        return this;
    }

    public XmsUnitOfWorkCacheItem<TValue> RemoveValue()
    {
        Value = default;
        IsRemoved = true;
        return this;
    }
}
public static class XmsUnitOfWorkCacheItemExtensions
{
    public static TValue GetUnRemovedValueOrNull<TValue>(this XmsUnitOfWorkCacheItem<TValue> item)
    {
        return item != null && !item.IsRemoved ? item.Value : default;
    }
}
